﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Model;
using MsDAL;
using System.ComponentModel;
using ServiceHelper;
using Components;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public partial class Sys0a05Manager : Sys0a05Base
    {
        #region 个人代码
        private Sys0a05DAL sys0a05Dal = new Sys0a05DAL();
        private static Sys0a05Manager _entity;

        public static Sys0a05Manager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new Sys0a05Manager();
                return _entity;
            }
        }

        /// <summary>
        /// 根据角色得到该角色拥有的权限
        /// </summary>
        /// <param name="rolecode"></param>
        /// <returns></returns>
        public DataTable GetPermissionByRoleCode(string rolecode)
        {
            return sys0a05Dal.GetAllPermission(rolecode, "");
        }

        public DataTable GetPermissionByUserCode(string usercode)
        {
            return sys0a05Dal.GetAllPermission("", usercode);
        }

        /// <summary>
        /// 得到当前登录用户的权限
        /// </summary>
        /// <returns>模块数据</returns>
        public object[] GetCurrentUserPermission1(string guid)
        {
            DataTable dt = sys0a05Dal.GetUserPermission(guid);
            if (dt == null || dt.Rows.Count == 0) return null;

            List<string> str = new List<string>();
            List<string> isRoot = new List<string>();
            foreach (DataRow dr in dt.Rows)
            {
                str.Add(dr["modulecode"].ToString());

                if ("1" == dr["isroot"].ToString()) isRoot.Add(dr["modulecode"].ToString());
            }

            return new object[] { str.ToArray(), isRoot.ToArray() };
        }

        /// <summary>
        /// 得到当前登录用户的权限
        /// </summary>
        /// <returns>模块数据</returns>
        public DataTable GetCurrentUserPermission(string guid)
        {
            return sys0a05Dal.GetUserPermission(guid);
        }

        /// <summary>
        /// 得到某个模块当前用户拥有的权限
        /// </summary>
        /// <returns>模块数据</returns>
        public string GetModulePermission(string moduleCode, string guid)
        {
            DataTable dt = sys0a05Dal.GetModulePermission(Context.GetUserCode(guid), moduleCode);
            if (dt == null || dt.Rows.Count == 0) return null;

            StringBuilder str = new StringBuilder();
            foreach (DataRow dr in dt.Rows)
            {
                if (str.Length != 0) str.Append(",");
                str.Append(dr["actioncode"].ToString());
            }

            return str.ToString();
        }

        /// <summary>
        /// 判断某个模块是否存在权限
        /// </summary>
        /// <param name="modulecode"></param>
        /// <returns></returns>
        public bool HasPermission(string modulecode, string guid)
        {
            DataTable dt = sys0a05Dal.GetModulePermission(guid, modulecode);
            return dt != null && dt.Rows.Count > 0;
        }

        public SqlApp DeleteByRolecode(string rolecode)
        {
            return sys0a05Dal.DeleteByRolecode(rolecode);
        }

        public SqlApp DeleteByUsercode(string usercode)
        {
            return sys0a05Dal.DeleteByUsercode(usercode);
        }

        //界面的回写方法
        public string SaveData(Hashtable ht, DataTable dtldata, DataTable deldata, string action, string usercode)
        {
            List<SqlApp> lst = new List<SqlApp>();
            Sys0a05 pRoot = null;

            string rolecode = Validation.ToString(ht["rolecode"]);

            //删除该角色的所有权限
            lst.Add(Sys0a05Manager.Entity.DeleteByRolecode(rolecode));

            if (null != dtldata)
            {
                foreach (DataRow tv in dtldata.Rows)//得到界面上用户选择的权限节点
                {
                    pRoot = new Sys0a05();//根节点
                    CommonUtil.GetInstance.CopyProperties(pRoot, tv);
                    lst.Add(this.Add(pRoot));
                }
            }

            return CommonManager.Entity.SaveData(lst);
        }

        //界面的回写方法
        public string SaveData_User(Hashtable ht, DataTable dtldata, DataTable deldata, string action, string ucode)
        {
            List<SqlApp> lst = new List<SqlApp>();
            Sys0a05 pRoot = null;

            string usercode = Validation.ToString(ht["usercode"]);

            //删除该角色的所有权限
            lst.Add(Sys0a05Manager.Entity.DeleteByUsercode(usercode));

            if (null != dtldata)
            {
                foreach (DataRow tv in dtldata.Rows)//得到界面上用户选择的权限节点
                {
                    pRoot = new Sys0a05();//根节点
                    CommonUtil.GetInstance.CopyProperties(pRoot, tv);
                    lst.Add(this.Add(pRoot));
                }
            }

            return CommonManager.Entity.SaveData(lst);
        }

        #endregion
    }
}
